package com.notary.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.notary.entity.vo.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import  com.notary.service.NotaryTemplateService;
import com.notary.entity.NotaryTemplate;

import java.util.Date;
import java.util.List;

/**
* <p>
* 公证词模板 前端控制器
* </p>
* @author xujun
* @since 2020-08-07
*/


@Api(tags = "公证词模板控制器")
@RequestMapping("/notaryTemplate")
@RestController
public class NotaryTemplateController{


    NotaryTemplateService notaryTemplateService;

    @Autowired
    public  NotaryTemplateController(NotaryTemplateService notaryTemplateService){
        this.notaryTemplateService = notaryTemplateService;
    }

    /**
    * <p>
        * 根据条件查询全部记录（并翻页）
        * </p>
    */

    @ApiOperation(value = "列表查询")
    @GetMapping("/list")
    public JsonResult<IPage<NotaryTemplate>> list(int pageNum,int pageSize,NotaryTemplate notaryTemplate) {
        Page<NotaryTemplate> page = new Page<NotaryTemplate>(pageNum,pageSize);

        QueryWrapper<NotaryTemplate>queryWrapper=new QueryWrapper();
        if(notaryTemplate.getType()!=null){
            queryWrapper.eq("type",notaryTemplate.getType());
        }
        IPage<NotaryTemplate> iPage = notaryTemplateService.page(page,queryWrapper);
        return JsonResult.success(iPage);
    }

    @ApiOperation(value = "列表查询")
    @GetMapping("/notaryTemplateList")
    public JsonResult<IPage<NotaryTemplate>> notaryTemplateList(int pageNum,int pageSize, Integer type, Long contractId) {
        Page<NotaryTemplate> page = new Page<NotaryTemplate>(pageNum,pageSize);
        List<NotaryTemplate> data = notaryTemplateService.notaryTemplateList(type, contractId);
        page.setRecords(data);
        return JsonResult.success(page);
    }


    /**
        *<P>
        * 根据ID查找返回单个对象
        *</P>
    */
    @ApiOperation(value = "根据id查询")
    @GetMapping("/find/{id}")
    public JsonResult<NotaryTemplate> getById(@PathVariable("id") Integer id) {
         return JsonResult.success(notaryTemplateService.getById(id));
    }


    /**
        *<p>
        * 保存对象-如果对象存在则修改原数据，否则插入一条新数据
        *</p>
    */
    @ApiOperation(value = "保存对象")
    @PostMapping("/saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody NotaryTemplate notaryTemplate) {

        if (notaryTemplate.getId() == null) {
            notaryTemplate.setCreateBy(notaryTemplate.getUpdateBy());
            notaryTemplate.setCreateTime(new Date());
            notaryTemplate.setUpdateTime(notaryTemplate.getCreateTime());
        }

        notaryTemplate.setUpdateTime(new Date());

        return JsonResult.success(notaryTemplateService.saveOrUpdate(notaryTemplate));
    }


    /**
        *<p>
        * 根据 id删除记录-逻辑删除
        *</p>
    */
    @ApiOperation(value = "删除")
    @GetMapping("/remove/{id}")
    public JsonResult removeById(@PathVariable("id") Long id) {
        return JsonResult.success(notaryTemplateService.removeById(id));
    }


}

